﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
 
using ZF.Model;
using ZF.BLL.Person;
namespace ZF.WS.Person
{
     
    public partial class OrderInfo : Order
    {
       
        [PageArgument(Key = "id", CanNull = false)]
        public int OrderId { get; set; }
        public Model.SP_Merchandise Merc { get; set; }
       
        public Model.SP_Order Order { get; set; }
        public Model.US_Member Member { get; set; }
        public string StatusName
        {
            get
            {

                return Order.StatusName;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
          
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var order = dc.SP_Order.FirstOrDefault(p => p.OrderId == this.OrderId);
                if (order == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                this.Order = order;
                var merc = order.SP_Merchandise;
                this.Merc = merc;
                this.Member = dc.US_Member.FirstOrDefault(p => p.MemberId == order.MemberId);
                if (this.Member == null)
                {
                    Member = new US_Member() { Account = "未找到该会员" };
                }
            }
            if (!IsPostBack)
            {
                Edit();
            }
        }
        public void InitControls()
        {
            this.tbConsignee.Text = this.Order.Consignee;
            this.tbTel.Text = this.Order.Tel;
            this.tbPhone.Text = this.Order.Phone;
            this.tbAddress.Text = this.Order.Address;
            this.tbZipcode.Text = this.Order.Zipcode;
            this.tbFreightPrice.Text = this.Order.FreightPrice.ToString();
            this.tbFreightType.Text = this.Order.FreightType.ToString();
            this.tbLastPrice.Text = this.Order.LastPrice.ToString();
            this.tbRecInfo.Text = this.Order.RecInfo;
            this.tbWaybillCompany.Text = this.Order.WaybillCompany;
            this.tbWaybillNO.Text = this.Order.WaybillNo;
            this.tbPubInfo.Text = this.Order.PubInfo;
            this.tbPaid.Text = string.Concat(this.Order.Paid);
            this.tbPayTime.Text = this.Order.PayTime.HasValue ? this.Order.PayTime.Value.ToShortDateString() : "";
            this.tbPaidInfo.Text = this.Order.PaidInfo;
            this.ddlPayType.SelectedValue = this.Order.PayType.ToString();
            this.tbRemark.Text = this.Order.Remark;
        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {


            //using (var dc = SiteInfo.CreateDBDataContext())
            //{
               

            //    var merc = new ZF.Model.SP_Merchandise()
            //    {
            //        Name = this.tbName.Text.Trim(),
            //        CPrice = this.tbCPrice.Text.Convert<decimal>(),
            //        Discount = this.ddlDiscount.SelectedValue == "-1" ? this.tbDiscount.Text.Convert<decimal>() : this.ddlDiscount.SelectedValue.Convert<decimal>(),
            //        AddTime = DateTime.Now,
            //        Hits = 0,
            //        Info = this.fckInfo.Value,
            //        EndTime = this.tbEndTime.Text.EmptyDateTime(),
            //        Freight =      this.tbFreight.Text.EmptyString("0").Convert<decimal>(),
            //        IsHot = this.cbIsHot.Checked,
            //        IsSell = this.cbIsSell.Checked,
            //        IsToPay = this.cbIsToPay.Checked,
            //        IsPromotions = this.cbIsPromotions.Checked,
            //        IsRecommend = this.cbIsRecommend.Checked,
            //        Param = this.tbParams.Text,
            //        Photo = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";"),
            //        Quantity = this.tbQuantity.Text.Convert<int>(),
            //        Tips = this.fckTips.Value,
            //        Unit = this.ddlUnit.SelectedValue == "自定义" ? this.tbUnit.Text : this.ddlUnit.SelectedValue,
            //        MerchandiseTypeId = this.ddlParams.SelectedValue.Convert<int>(),
            //        Integral = this.tbIntegral.Text.EmptyInt(0),
            //        MPrice = this.tbMPrice.Text.Convert<decimal?>(),
            //        Distribution = this.tbDistribution.Text,
            //        No=Guid.NewGuid().ToString(),

            //    };


            //    try
            //    {
            //        dc.SP_Merchandise.InsertOnSubmit(merc);
            //        dc.SubmitChanges();
            //        if (this.cbAutoNo.Checked)
            //            merc.No = (10000 + merc.MerchandiseId).ToString();
            //        else
            //            merc.No = this.tbNo.Text;
            //        dc.SubmitChanges();
            //    }
            //    catch (Exception ex)
            //    {
            //        throw WarnFactory.CreateWarn(ex);
            //    }
            //    string msgKey = CTitle;

            //    SiteInfo.InsertLog("添加" + msgKey, null);
            //    WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
            //    link.AddWarnMessageLink(string.Format("继续添加{0}", msgKey), Request.Url.ToString());
            //    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

            //}

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                InitControls();
            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            //using (var ts = new System.Transactions.TransactionScope())
            //{
            //    using (var dc = BLL.SiteInfo.CreateDBDataContext())
            //    {
            //        var merc = dc.SP_Merchandise.FirstOrDefault(p => p.MerchandiseId == this.MerchandiseId);
            //        if (merc == null)
            //            throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
            //        merc.Name = this.tbName.Text.Trim();
            //        merc.MPrice = this.tbMPrice.Text.Convert<decimal?>();
            //        merc.CPrice = this.tbCPrice.Text.Convert<decimal>();
            //        merc.Discount = this.ddlDiscount.SelectedValue == "-1" ? this.tbDiscount.Text.Convert<decimal>() : this.ddlDiscount.SelectedValue.Convert<decimal>();
                   
          
            //        merc.Info = this.fckInfo.Value;
            //        merc.EndTime = this.tbEndTime.Text.EmptyDateTime();
            //        merc.Freight = this.tbFreight.Text.EmptyString("0").Convert<decimal>();
            //        merc.IsHot = this.cbIsHot.Checked;
            //        merc.IsSell = this.cbIsSell.Checked;
            //        merc.IsToPay = this.cbIsToPay.Checked;
            //        merc.IsPromotions = this.cbIsPromotions.Checked;
            //        merc.IsRecommend = this.cbIsRecommend.Checked;
            //        merc.Param = this.tbParams.Text;
    

            //        var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            //        photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
            //        merc.Photo = photoList.ConstituteString(";");



            //        merc.Quantity = this.tbQuantity.Text.Convert<int>();
            //        merc.Tips = this.fckTips.Value;
            //        merc.Unit = this.ddlUnit.SelectedValue == "自定义" ? this.tbUnit.Text : this.ddlUnit.SelectedValue;
            //        merc.MerchandiseTypeId = this.ddlParams.SelectedValue.Convert<int>();
            //        merc.Integral = this.tbIntegral.Text.EmptyInt(0);
                
            //        merc.Distribution = this.tbDistribution.Text;
 
            //        try
            //        {
            //            dc.SubmitChanges();
            //            ts.Complete();
            //        }
            //        catch (Exception ex)
            //        {
            //            throw WarnFactory.CreateWarn(ex);
            //        }
            //        string msgKey = CTitle;

            //        SiteInfo.InsertLog("修改" + msgKey, null);
            //        WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回{0}列表", msgKey));

            //        link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
            //        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

            //    }


            //}

        }
        public void FSReset(object sender, EventArgs e)
        {

            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var order = dc.SP_Order.FirstOrDefault(p => p.OrderId == this.OrderId);
                if (order == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                order.FreightType = this.tbFreightType.Text.Convert<int>();
                order.FreightPrice = this.tbFreightPrice.Text.Convert<decimal>();
                order.Remark = this.tbRemark.Text;
                dc.SubmitChanges();
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = "修改成功",
                    Link= WarnLink.Current,
                }).Alert();
            }

        }
        public void LastPriceSet(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var order = dc.SP_Order.FirstOrDefault(p => p.OrderId == this.OrderId);
                if (order == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                string msg = "";
                if (!order.IsPayed)
                {
                    order.LastPrice = this.tbLastPrice.Text.Convert<decimal>();
                    order.Remark = this.tbRemark.Text;
                    dc.SubmitChanges();
                    msg = "修改价格成功";
                }
                else
                    msg = "订单已支付，不能修改价格";
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = msg,
                    Link = WarnLink.Current,
                }).Alert();

            }
        }
        public void RecInfoSet(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var order = dc.SP_Order.FirstOrDefault(p => p.OrderId == this.OrderId);
                if (order == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                order.Consignee = this.tbConsignee.Text;
                order.Tel = this.tbTel.Text;
                order.Phone = this.tbPhone.Text;
                order.Address = this.tbAddress.Text;
                order.Zipcode = this.tbZipcode.Text;
                order.Remark = this.tbRemark.Text;
                dc.SubmitChanges();
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = "修改成功",
                    Link = WarnLink.Current,

                }).Alert();
            }
        }
        public void SendInfoSet(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var order = dc.SP_Order.FirstOrDefault(p => p.OrderId == this.OrderId);
                if (order == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                string msg = "";
                if (order.PayType == 2 || order.IsPayed)
                {
                    order.WaybillCompany = this.tbWaybillCompany.Text;
                    order.WaybillNo = this.tbWaybillNO.Text;
                    order.PubInfo = this.tbPubInfo.Text;
                    order.IsSend = true;
                    order.Status = (int)OrderStatus.处理中30;
                    order.Remark = this.tbRemark.Text;
                    dc.SubmitChanges();
                    msg = "操作成功，订单已发货";
                }
                else
                {
                    msg = "操作失败。选择在线支付，必须先支付之后才能发货";
                }
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = msg,
                    Link = WarnLink.Current,

                }).Alert();
            }
        }
        public void PaidSet(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var order = dc.SP_Order.FirstOrDefault(p => p.OrderId == this.OrderId);
                if (order == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                string msg = "";
                if (order.PayType == (int)PayType.ToPay && order.IsSend)
                {
                    order.Paid = this.tbPaid.Text.Convert<decimal>();
                    order.PayTime = this.tbPayTime.Text.Convert<DateTime>();
                    order.PaidInfo = this.tbPaidInfo.Text;
                    order.IsPayed = true;
                    order.Status = (int)OrderStatus.订单已完成40;
                    order.Remark = this.tbRemark.Text;
                    dc.SubmitChanges();
                    msg = "操作成功，该订单已完成";
                }
                else
                {
                    if(order.PayType != (int)PayType.ToPay)
                        msg = "操作失败，支付方式必须为货到付款才能设置收款信息";
                    else if(!order.IsSend)
                        msg = "操作失败，当前订单未设置发件信息，不能进行收款操作";
                }
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = msg,
                    Link = WarnLink.Current,
                }).Alert();
            }
        }
        public void PayTypeSet(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var order = dc.SP_Order.FirstOrDefault(p => p.OrderId == this.OrderId);
                if (order == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                string msg = "";
                if (!order.IsPayed && !order.IsSend)
                {
                    order.PayType = this.ddlPayType.SelectedValue.Convert<int>();
                    order.Remark = this.tbRemark.Text;
                    dc.SubmitChanges();
                    msg = "修改支付方式成功";
                }
                else
                {
                    if (order.IsPayed)
                        msg = "订单已支付，不能修改支付方式";
                    else if (order.IsPayed)
                        msg = "订单已发货，不能修改支付方式";
                }
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = msg,
                    Link = WarnLink.Current,
                }).Alert();
            }
        }
        public void Affirm(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            var id = Order.OrderId;

            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var order = dc.SP_Order.FirstOrDefault(p => p.OrderId == id);
                if (order == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                string msg = "";
                if (order.Status == (int)OrderStatus.下单10)
                {
                    order.Status = (int)OrderStatus.确认20;
                    order.Remark = this.tbRemark.Text;
                    dc.SubmitChanges();
                    msg = "确认订单成功";
                }
                else
                    msg = "确认订单失败";
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = msg,
                    Link = WarnLink.Current,
                }).Alert();
            }
            
        }
        
         
    }
 
}
